//@author A0106044W
package logic;

import java.util.ArrayList;

import commandInfo.CommandInfo;
import commandInfo.StatusFilterCommandInfo;
import storage.Task;

/**
 * 
 *
 * 	       Sothearith Sreang
 *         All of the Logic part is written by me.
 * 
 */

public class CompleteFilterHandler extends OperationHandler {
	
	ArrayList<Task> filterTasksList;
	ArrayList<Boolean> taskStatusList;
	
	private static final String SUCCESSFUL_COMPLETE_MESSAGE = "%1d task%s are now successfully set to complete";
	private static final String SUCCESSFUL_UNDO_MESSAGE = "%1d task%s are successful set to their previous statuses";
	
	private static final String COMMAND_PENDING = "PENDING";
	private static final String COMMAND_COMPLETE = "COMPLETE";
	
	// Two public constructors
	
	//for normal filter complete operation
	public CompleteFilterHandler(ArrayList<Task> taskList){
		super();
		initializeList();
		filterTasksList = taskList;
	}
	
	// for undo, redo filter complete operation
	public CompleteFilterHandler(CommandInfo commandInfo){
		super();
		initializeList();
		setUpTasksList(commandInfo);
	}
	
	// This method is for user command's filter complete operation
	public String execute() {
		for(Task task: filterTasksList) {
			taskStatusList.add(task.getDoneStatus());
			task.setDone();
		}
		super.commandInfo = new StatusFilterCommandInfo(COMMAND_COMPLETE, filterTasksList, taskStatusList);
		String feedback = Helper.setUpFeedback(SUCCESSFUL_COMPLETE_MESSAGE, filterTasksList.size());
		
		return feedback;
	}
	
	// This method is for undo operation for previous filter complete operation
	public String undo() {
		ArrayList<Boolean> statusList = new ArrayList<Boolean>();
		
		for(int index=0; index<filterTasksList.size(); index++) {
			Task task = filterTasksList.get(index);
			boolean status = taskStatusList.get(index);
			
			task.setStatus(status);
			statusList.add(true);
		}
		super.commandInfo = new StatusFilterCommandInfo(COMMAND_PENDING, filterTasksList, statusList);
		String feedback =  Helper.setUpFeedback(SUCCESSFUL_UNDO_MESSAGE, filterTasksList.size());
		
		return feedback;
	}
	
	// Next level abstraction
	// These methods are called by the constructor
	
	private void initializeList() {
		filterTasksList = new ArrayList<Task>();
		taskStatusList = new ArrayList<Boolean>();
	}
	
	private void setUpTasksList(CommandInfo commandInfoP){
		ArrayList<Task> cloneTasksList = commandInfoP.getUpdatedTasksList();
		taskStatusList = commandInfoP.getStatusList();
		
		for(Task cloneTask: cloneTasksList) {
			Task task = super.getTask(cloneTask);
			assert task != null;
			filterTasksList.add(task);
		}
	}

}
